from fastapi import FastAPI,Request
import time
from routers import register_router
from models import register_database

app = FastAPI()

def creat_app():
    # 注册路由
    register_router(app)
    # 注册数据库
    register_database()

    @app.middleware("http")
    # 定义中间件功能
    async def add_process_time_header(request: Request, call_next):
        start_time = time.time()
        response = await call_next(request)
        process_time = round((time.time() - start_time) * 1000, 2)
        response.headers["X-Process-Time"] = f'{str(process_time)}ms'
        return response

    return app